<script setup lang="ts">
import { ElMessage } from 'element-plus'
import ModelResetConfirm from '@/components/ModelResetConfirm/index.vue'

const chemicalStore = useChemicalStore()

const tileSetOptions: any = chemicalStore.tileSetOptions

const jdMin = ref(0)
jdMin.value = accSub(tileSetOptions.tx, 0.2)

const jdMax = ref(0)
jdMax.value = accAdd(tileSetOptions.tx, 0.2)

const wdMin = ref(0)
wdMin.value = accSub(tileSetOptions.ty, 0.2)

const wdMax = ref(0)
wdMax.value = accAdd(tileSetOptions.ty, 0.2)

const gdMin = ref(0)
gdMin.value = accSub(tileSetOptions.tz, 50)

const gdMax = ref(0)
gdMax.value = accAdd(tileSetOptions.tz, 50)

const adjustClick = (type: any) => {
  switch (type) {
    // 经度调整 加
    case 'jdPlus':
      tileSetOptions.tx = accAdd(tileSetOptions.tx, 0.00001)
      break
    // 经度调整 减
    case 'jdReduce':
      tileSetOptions.tx = accSub(tileSetOptions.tx, 0.00001)
      break
    // 纬度调整 加
    case 'wdPlus':
      tileSetOptions.ty = accAdd(tileSetOptions.ty, 0.00001)
      break
    // 纬度调整 减
    case 'wdReduce':
      tileSetOptions.ty = accSub(tileSetOptions.ty, 0.00001)
      break
  }
}

// 加法函数
function accAdd(arg1: any, arg2: any) {
  let r1 = 0
  let r2 = 0
  let m
  let c

  try {
    r1 = String(arg1).split('.')[1].length
  } catch (e) {}

  try {
    r2 = String(arg2).split('.')[1].length
  } catch (e) {}

  m = Math.pow(10, Math.max(r1, r2))
  arg1 = arg1 * m
  arg2 = arg2 * m

  return (arg1 + arg2) / m
}

// 减法函数
function accSub(arg1: any, arg2: any) {
  let r1 = 0
  let r2 = 0
  let m
  let c

  try {
    r1 = String(arg1).split('.')[1].length
  } catch (e) {}

  try {
    r2 = String(arg2).split('.')[1].length
  } catch (e) {}

  m = Math.pow(10, Math.max(r1, r2))
  arg1 = arg1 * m
  arg2 = arg2 * m

  return (arg1 - arg2) / m
}

//复位
const reset = () => {
  chemicalStore.modelResetConfirmShow = true
}

//模型复位
const modelReset = () => {
  updateMxdzInfo({
    id: chemicalStore.qyInfo.mxdzInfoList[0].id,
    mxsz: '',
  })
    .then((res: any) => {
      ElMessage.success('复位成功')

      chemicalStore.tileSetOptions.tx = chemicalStore.modelOldWzxx.tx
      chemicalStore.tileSetOptions.ty = chemicalStore.modelOldWzxx.ty
      chemicalStore.tileSetOptions.tz = chemicalStore.modelOldWzxx.tz

      chemicalStore.qyInfo.mxdzInfoList[0].mxsz = ''

      window.GCamera.flyTo({
        pos: {
          lng: chemicalStore.modelOldWzxx.tx,
          lat: chemicalStore.modelOldWzxx.ty,
          height: 1200,
        },
      })
    })
    .catch(() => {
      ElMessage.error('复位失败')
    })
}

//保存
const comfirm = () => {
  updateMxdzInfo({
    id: chemicalStore.qyInfo.mxdzInfoList[0].id,
    mxsz: JSON.stringify(tileSetOptions),
  })
    .then((res: any) => {
      ElMessage.success('模型设置数据保存成功')
      chemicalStore.qyInfo.mxdzInfoList[0].mxsz = tileSetOptions
    })
    .catch(() => {
      ElMessage.error('模型设置数据保存失败')
    })
}
</script>

<template>
  <div class="azimuthAdjustment">
    <div class="azimuthAdjustment-header">模型设置</div>
    <div style="padding: 30px 15px 15px">
      <div class="map-overlay-inner">
        <div class="map-overlay-item">
          <label
            >经度调整: <span>{{ tileSetOptions.tx.toFixed(6) }}</span></label
          >
          <div class="jiajian-btn">
            <el-button @click="adjustClick('jdReduce')">-</el-button>
            <el-button @click="adjustClick('jdPlus')">+</el-button>
          </div>
        </div>
        <div>
          <!-- <input
            id="slider-x"
            type="range"
            min="0.0"
            max="180.0"
            step="0.00001"
            value="103.8508768"
          /> -->
          <el-slider v-model="tileSetOptions.tx" :step="0.000001" :min="jdMin" :max="jdMax" />
        </div>
      </div>
      <div class="map-overlay-inner">
        <div class="map-overlay-item">
          <label
            >纬度调整: <span>{{ tileSetOptions.ty.toFixed(6) }}</span></label
          >
          <div class="jiajian-btn">
            <el-button @click="adjustClick('wdReduce')">-</el-button>
            <el-button @click="adjustClick('wdPlus')">+</el-button>
          </div>
        </div>
        <div>
          <!-- <input
            id="slider-y"
            type="range"
            min="0.0"
            max="90.0"
            step="0.00001"
            value="36.0705099"
          /> -->
          <el-slider v-model="tileSetOptions.ty" :step="0.000001" :min="wdMin" :max="wdMax" />
        </div>
      </div>
      <div class="map-overlay-inner">
        <div class="map-overlay-item">
          <label
            >调整高度: <span>{{ tileSetOptions.tz.toFixed(2) }}</span></label
          >
        </div>
        <div>
          <el-slider v-model="tileSetOptions.tz" :min="gdMin" :max="gdMax" :step="0.01" />
        </div>
      </div>
      <div class="azimuthAdjustment-bottom-btn">
        <el-button type="info" @click="reset">复&nbsp;&nbsp;位</el-button>
        <el-button type="info" style="background: #02979b; margin-left: 60px" @click="comfirm"
          >保&nbsp;&nbsp;存</el-button
        >
      </div>
    </div>
  </div>
  <ModelResetConfirm @modelReset="modelReset" />
</template>

<style lang="less" scoped>
.azimuthAdjustment {
  position: absolute;
  top: 112px;
  right: 10px;
  height: calc(100% - 122px);
  z-index: 10;
  width: 460px;

  border: 1px solid #0d609c;
  color: #fff;
  background: rgba(1, 7, 17, 0.7);
  box-sizing: border-box;

  .azimuthAdjustment-header {
    height: 50px;
    line-height: 50px;
    padding-left: 15px;
    border-bottom: 1px solid grey;
    font-size: 18px;
    font-weight: 700;
    box-sizing: border-box;
  }
  .map-overlay-inner {
    padding: 15px;
    margin-bottom: 40px;
    background-color: rgba(109, 109, 109, 0.2);
    border: 1px solid #6d6d6d;
    .map-overlay-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      label {
        font-size: 16px;
        color: #fff;
        span {
          font-size: 15px;
          color: #fff;
        }
      }
    }
    input {
      background-color: transparent;
      display: inline-block;
      width: 100%;
      position: relative;
      margin-top: 10px;
      cursor: ew-resize;
    }
    /deep/ .el-button {
      padding: 10px 15px;
      background-color: #f9fafb;
    }
  }

  .jiajian-btn {
    /deep/.el-button {
      width: 26px;
      height: 26px;
      padding: 0;
      background-color: #d7d7d7;
      span {
        font-size: 30px;
        color: #000;
      }
    }
  }

  /deep/.el-slider__bar {
    background-color: #02979b;
  }

  /deep/.el-slider__runway {
    background-color: #7f7f7f;
  }

  .azimuthAdjustment-bottom-btn {
    position: absolute;
    bottom: 15px;
    width: 100%;
    text-align: center;
    padding: 17px 0;
    .el-button {
      width: 140px;
      height: 30px;
      font-size: 18px;
      background: #778082;
      border: 0;
      border-radius: 10px;
      cursor: pointer;
    }
  }
}
</style>
